<template>
  <div>
    <!-- 评论列表 -->
    <div class="cmt-list" :class="isCommentShow ? 'art-cmt-container-1' : 'art-cmt-container-2'">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
        :immediate-check="false"
      >
        <!-- 评论的 Item 项 -->
        <div class="cmt-item" v-for="obj in commentList" :key="obj.com_id">
          <!-- 头部区域 -->
          <div class="cmt-header">
            <!-- 头部左侧 -->
            <div class="cmt-header-left">
              <img :src="obj.aut_photo" alt="" class="avatar" />
              <span class="uname">{{ obj.aut_name }}</span>
            </div>
            <!-- 头部右侧 -->
            <div class="cmt-header-right">
              <van-icon
                name="like"
                size="16"
                color="red"
                v-if="obj.is_liking === true"
                @click="loveFn(false, obj)"
              />
              <van-icon
                name="like-o"
                size="16"
                color="gray"
                v-else
                @click="loveFn(true, obj)"
              />
            </div>
          </div>
          <!-- 主体区域 -->
          <div class="cmt-body">
            {{ obj.content }}
          </div>
          <!-- 尾部区域 -->
          <div class="cmt-footer">{{ formatTimeFn(obj.pubdate) }}</div>
        </div>
      </van-list>
    </div>

    <!-- 底部添加评论区域 - 1 -->
    <div class="add-cmt-box van-hairline--top" v-if="isCommentShow">
      <van-icon name="arrow-left" size="0.48rem" @click="$router.back()" />
      <div class="ipt-cmt-div" @click="isCommentShow = false">发表评论</div>
      <div class="icon-box">
        <van-badge :content="comCount" max="99">
          <van-icon name="comment-o" size="0.53333334rem" @click="moveFn"/>
        </van-badge>
        <van-icon name="star-o" size="0.53333334rem" />
        <van-icon name="share-o" size="0.53333334rem" />
      </div>
    </div>

    <!-- 底部添加评论区域 - 2 -->
    <div class="cmt-box van-hairline--top" v-else>
      <textarea v-fofo placeholder="友善评论、理性发言、阳光心灵" @blur="blurFn" v-model="sendText"></textarea>
      <van-button type="default" :disabled="sendText.length === 0" @click="sendFn">发布</van-button>
    </div>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import zh from 'dayjs/locale/zh-cn'
import {
  getArticleCommentListAPI,
  commentLikingAPI,
  commentDisLikingAPI,
  sendCommentAPI
} from '@/api'
dayjs.locale(zh)
dayjs.extend(relativeTime)
export default {
  data () {
    return {
      commentList: [], // 评论列表
      isCommentShow: true, // 评论区域1和2的显示/隐藏
      comCount: '', // 记录评论数量
      sendText: '', // 发布评论内容
      loading: false,
      finished: false,
      query: { // 获取评论列表, 传参对象
        artId: ''
      }
    }
  },
  created () {
    this.query.artId = this.$route.query.art_id // 在请求发起之前, 把路由上文章id保存到参数对象里
    this.getArticleCommentListFn()
  },
  methods: {
    // 获取-文章评论列表
    async getArticleCommentListFn () {
      const res = await getArticleCommentListAPI(this.query)
      // console.log(res)
      this.commentList = [...this.commentList, ...res.data.data.results]
      this.comCount = res.data.data.total_count || '' // 保存评论总数 (没有评论数量为0时, 我想给小微标一个空字符串)

      this.query.offset = res.data.data.last_id // 每次请求后, 都把最新的last_id存一下, 方便下次load调用加载更多时, 请求下一段10条评论数据
      this.loading = false
      const { data: { data: { last_id, results } } } = res
      if (last_id === null || results.length === 0) {
        this.finished = true
      }
    },
    // 格式化-时间
    formatTimeFn (dateStr) {
      // 指定的日期字符串
      const str = dayjs(dateStr).fromNow()
      return str
    },
    // 评论右侧的小心心->点击事件
    async loveFn (bool, obj) {
      // obj的值: 评论对象
      if (bool === true) {
        // 点亮逻辑
        obj.is_liking = true
        await commentLikingAPI({
          comId: obj.com_id
        })
      } else {
        // 取消点亮逻辑
        obj.is_liking = false
        await commentDisLikingAPI({
          comId: obj.com_id
        })
      }
    },
    // 发布评论-失焦事件
    blurFn () {
      setTimeout(() => {
        this.isCommentShow = true
      }, 0)
    },
    // 点击评论小图标
    moveFn () {
      // 让底部评论组件滚动上来
      // scrollIntoView()方法, 原生DOM的方法, 可以让调用者原生标签, 自动滚动到视口顶端出现
      // 获取父级组件内标签(可以的), 这些组件标签最终都会挂载到真实DOM上
      document.querySelector('.like-box').scrollIntoView({
        behavior: 'smooth' // 属性和值固定->让滚动带动画
      })
    },
    // 发布评论->点击事件
    async sendFn () {
      const res = await sendCommentAPI({
        artId: this.$route.query.art_id,
        content: this.sendText
      })
      console.log(res)
      this.commentList.unshift(res.data.data.new_obj)
      this.sendText = ''
      this.comCount++ // 数量+1

      // 问题: 点击发布后, 点击事件没执行
      // 原因: v-if和v-else, 整个div被销毁了, 还没有来得及vue触发@click事件方法
      // 解决: 失焦里代码慢点执行(为何0秒-请复习事件循环)
    },
    // 评论列表触底加载更多
    onLoad () {
      if (this.commentList.length > 0) {
        this.getArticleCommentListFn()
      }
    }
  }
}
</script>

<style scoped lang="less">
.cmt-list {
  padding: 10px;
  .cmt-item {
    padding: 15px 0;
    + .cmt-item {
      border-top: 1px solid #f8f8f8;
    }
    .cmt-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .cmt-header-left {
        display: flex;
        align-items: center;
        .avatar {
          width: 40px;
          height: 40px;
          background-color: #f8f8f8;
          border-radius: 50%;
          margin-right: 15px;
        }
        .uname {
          font-size: 12px;
        }
      }
    }
    .cmt-body {
      font-size: 14px;
      line-height: 28px;
      text-indent: 2em;
      margin-top: 6px;
      word-break: break-all;
    }
    .cmt-footer {
      font-size: 12px;
      color: gray;
      margin-top: 10px;
    }
  }
}

/*美化 - 文章详情 - 底部的评论页面 */
// 外层容器
.art-cmt-container-1 {
  padding-bottom: 46px;
}
.art-cmt-container-2 {
  padding-bottom: 80px;
}

// 发布评论的盒子 - 1
.add-cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  box-sizing: border-box;
  background-color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 46px;
  line-height: 46px;
  padding-left: 10px;
  .ipt-cmt-div {
    flex: 1;
    border: 1px solid #efefef;
    border-radius: 15px;
    height: 30px;
    font-size: 12px;
    line-height: 30px;
    padding-left: 15px;
    margin-left: 10px;
    background-color: #f8f8f8;
  }
  .icon-box {
    width: 40%;
    display: flex;
    justify-content: space-evenly;
    line-height: 0;
  }
}

.child {
  width: 20px;
  height: 20px;
  background: #f2f3f5;
}

// 发布评论的盒子 - 2
.cmt-box {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  padding-left: 10px;
  box-sizing: border-box;
  background-color: white;
  textarea {
    flex: 1;
    height: 66%;
    border: 1px solid #efefef;
    background-color: #f8f8f8;
    resize: none;
    border-radius: 6px;
    padding: 5px;
  }
  .van-button {
    height: 100%;
    border: none;
  }
}
</style>
